<?php
/**
 * Handles display of a navigation menu defined in the admin panel
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;
use application\system\Permalink;
class NavMenu {
	
	/**
	 * Class applied to the ul
	 *
	 * @var string
	 */
	protected $ulClass;
	
	/**
	 * Show links or just names?
	 *
	 * @var bool
	 */
	protected $links;
	
	/**
	 * Menu to display
	 *
	 * @var string
	 */
	protected $menu;
	
	/**
	 * Depth of the menu
	 *
	 * @var int
	 */
	protected $levels;
	
	/**
	 * Text before each link in the li
	 *
	 * @var string
	 */
	protected $before;
	
	/**
	 * Text after each link in the li
	 *
	 * @var string
	 */
	protected $after;
	
	
	/**
	 * Display a navigation menu with the provided properties
	 *
	 * @param string $menu The menu to display
	 * @param bool $links Links or just text?
	 * @param bool $homeLink Put a link to the home page in front of all other links
	 * @param bool $li Return menu items in an <li> or in an array
	 * @param string $before String before each link
	 * @param string $after String after each link
	 * @param string $separator String between each <li>
	 */
	public function navigationMenu($menu, $links = TRUE, $homeLink = FALSE, $li = TRUE, $before = '', $after = '', $separator = '') {
		global $concerto;
		$urlInfo = $concerto->UrlInterpreter->urlInfo;
		
		$this->ulClass = $ulClass;
		$this->menu = $menu;
		$this->links = $links;
		$this->before = $before;
		$this->after = $after;
		if($homeLink) {
			$class = (isHome()) ? ' class="current"' : '';
			echo '<li'.$class.'><a href="'.CN_URL.'">Home</a></li>';
		}
		
		if(!$li) $menuDisplay = array();
		
		$this->levels = DB()->getVar("SELECT MAX(`nav_depth`) FROM `cn_nav_menus` WHERE `nav_menu`='$menu' GROUP BY `nav_menu`");
		/** Check if it's active (is active page, or is menu-parent of an active page) */
			$current = $urlInfo[count($urlInfo) - 1];
		
		$parents = DB()->getResults("SELECT * FROM `cn_nav_menus` LEFT JOIN `cn_content` ON `nav_resource`=`content_id` LEFT JOIN `cn_tags` ON `nav_resource`=`tag_id` LEFT JOIN `cn_content_types` ON `nav_resource`=`ct_id` WHERE `nav_menu`='$this->menu' AND `nav_depth`='1' ORDER BY `nav_order`");
		foreach($parents as $parent) {
			if($li) $menuDisplay = null;
			$children = $this->getChildMenuElements($parent->nav_id);
			$before = $this->before;
			$after = $this->after;
			if($links) {
				$before .= '<a href="'.Permalink::buildPermalink($parent->nav_resource_type, $parent->nav_resource, $parent->content_type).'">';
				$after = '</a>'.$after;
			}
			
			
			
			$class = '';
			
			/** If it's in the URL, or is an ancestor of the current resource, highlight it */
			foreach($urlInfo as $info) {
				if(($info['resource'] == $parent->nav_resource_type && $info['id'] == $parent->nav_resource) || $children[1] == TRUE) {
					$class = ' class="current"';
				}
			}
			
			if($parent->nav_resource_type == 'listing') {
				$childListings = Content::getDescendants($parent->nav_resource, 1);
				$childListings[] = $parent->nav_resource;
				if(in_array(getQueryVar('id'), $childListings) || in_array(getQueryVar('child_of'), $childListings) ) {
					$class = ' class="current"';
				}
			}
			
			
			if($li) {
				$menuDisplay .= $indent.'<li'.$class.'>'.$before.$parent->nav_display_name.$after;
				$menuDisplay .= $children[0];
				$menuDisplay .= '</li>';
				
				echo $menuDisplay;
			}
			else {
				$menuDisplay[] = $before.$parent->nav_display_name.$after;
			}
		}
		if(!$li) {
			echo implode($separator, $menuDisplay);
		}
		
	}
	
	/**
	 * Get all items in the menu that are children of the parent <li>
	 *
	 * @param int The parent's database ID
	 * @return string The <ul> of all child elements
	 */
	protected function getChildMenuElements($parent) {
		global $concerto;
		$urlInfo = $concerto->UrlInterpreter->urlInfo;
			
			
		$children = DB()->getResults("SELECT * FROM `cn_nav_menus` LEFT JOIN `cn_content` ON `nav_resource`=`content_id` LEFT JOIN `cn_tags` ON `nav_resource`=`tag_id` LEFT JOIN `cn_content_types` ON `nav_resource`=`ct_id` WHERE `nav_menu`='$this->menu' AND `nav_parent`='$parent' ORDER BY `nav_order`");
		if(!count($children)) return;
		if($this->ulClass) $ulClass = ' class="'.$ulClass.'"';
		$menuDisplay = '<ul'.$ulClass.'>';
		$active = FALSE;
		foreach($children as $child) {
			$before = $this->before;
			$after = $this->after;
			if($this->links) {
				$before .= '<a href="'.Permalink::buildPermalink($child->nav_resource_type, $child->nav_resource, $child->content_type).'">';
				$after = '</a>'.$after;
			}
			$class = '';
			foreach($urlInfo as $info) {
				if($info['resource'] == $child->nav_resource_type && $info['id'] == $child->nav_resource) {
					$class = ' class="current"';
					$active = TRUE;
				}
			}
			$menuDisplay .= $indent.'<li'.$class.'>'.$before.$child->nav_display_name.$after;
			$menuDisplay .= $this->getchildMenuElements($child->nav_id);
			$menuDisplay .= '</li>';
		}
		$menuDisplay .= '</ul>';
		return array($menuDisplay, $active);
	}


}

?>